#!/bin/sh
# Tanken Plugin
# by theobald123 for coolstream
# VERSION=1.7.1

# *************************************************************************************
# *      Datenzeile in einzelne Parameter aufteilen                                   *
# *-----------------------------------------------------------------------------------*
# *      bergabeparameter : Datenzeile                                               *
# *************************************************************************************
Parameter ()
{ pn=$#;p1=$1;p2=$2;p3=$3;p4=$4;p5=$5;p6=$6;p7=$7;p8=$8;p9=$9;p10=$10; }

# *************************************************************************************
# *      exit-Steuerung                                                               *
# *************************************************************************************
Beenden ()
{
if [ $? = 0 ]; then
	break
fi
}

# *************************************************************************************
# *      Einlesen der Postleitzahl                                                    *
# *-----------------------------------------------------------------------------------*
# *      bergabeparameter : Postleitzahlendatei                                      *
# *************************************************************************************
getplz ()
{
conf=$1
if [ -s $conf ] ; then
	Eingabe1=`cat "$conf"`
else
	Eingabe1="0"
fi

# Eingabemaske
Eingabe=`input l="PLZ:     #####max. Entf.: ##" o=45 c=14 d="$Eingabe1"`

if [ "$Eingabe" = "" ] ; then
	exit
else
	echo "$Eingabe" > $conf
fi

PLZ=`cut -b  1-5 $conf`; PLZ=`echo $PLZ`
if [ ${#PLZ} -lt 5 ] ; then
	msgbox size=22 timeout=5 popup="PLZ ungueltig"
#	wget -q -O - http://127.0.0.1/control/message?popup="PLZ ungueltig"
	rm $conf
	continue
fi

ENTF=`cut -b 6-7 $conf`; ENTF=`echo $ENTF`
if [ "$ENTF" = "" ] ; then
	ENTF=5
elif [ $ENTF -lt 1 ] ; then
	ENTF=5
fi
}

#while :; do
# *-----------------------------------------------------------------------------------*
# *      Kraftstoffauswahl und Tanken anfordern                                       *
# *-----------------------------------------------------------------------------------*
	getplz /var/tuxbox/config/Tanken.plz
	auswahl=1
	#while :; do
		msgbox title="Kraftstoffauswahl" size=28 order=1 msg="~cBitte ausw~ahlen!" select="Diesel,Super E10,Super (E5),SuperPlus,Autogas (LPG),Erdgas (CNG),Biodiesel,LKW-Diesel" default=$auswahl
		auswahl=$?
		case $auswahl	in
		1)
			Typ=3; SPRIT="Diesel"
			;;
		2)
			Typ=5; SPRIT="Super E10"
			;;
		3)
			Typ=7; SPRIT="Super (E5)"
			;;
		4)
			Typ=6; SPRIT="SuperPlus"
			;;
		5)
			Typ=1; SPRIT="Autogas (LPG)"
			;;
		6)
			Typ=8; SPRIT="Erdgas (CNG)"
			;;
		7)
			Typ=9; SPRIT="Biodiesel"
			;;
		8)
			Typ=2; SPRIT="LKW-Diesel"
			;;
		*)
			break
			;;
		esac

		wget -O /tmp/tanken.txt -U Mozilla "https://www.clever-tanken.de/tankstelle_liste?spritsorte=$Typ&ort=$PLZ&r=$ENTF&sort=p"

# *-----------------------------------------------------------------------------------*
# *      #HTML-Tags entfernen; Sonderzeichen ersetzen; Leerzeilen entfernen           *
# *-----------------------------------------------------------------------------------*
		sed -n -e "/main-column-container/,/footer-container/ p" /tmp/tanken.txt \
		    -n -e "/Abfrage leider keine Tankstelle finden/ p"  | \
		sed -n -e "/<a/,/<\/a>/ p" \
			-n -e "/Abfrage leider keine Tankstelle finden/ p"  | \
		sed -n -e "/location-name/ p" \
		    -n -e "/location-street/ p" \
		    -n -e "/location-city/ p" \
			-n -e "/<sup/ p" \
			-n -e "/ffnet/ p" \
			-n -e "/Abfrage leider keine Tankstelle finden/ p" | \
		sed -e 's/<[^>]*>//g' \
			-e '/^[^0-9a-zA-Z!+-\/]*$/d' | \
		sed -e 's/ä//g' -e 's/&auml;//g'  -e 's/&#228;//g' \
			-e 's/ö//g' -e 's/&ouml;//g'  -e 's/&#246;//g' \
			-e 's/ü//g' -e 's/&uuml;//g'  -e 's/&#252;//g' \
			-e 's/Ä//g' -e 's/&Auml;//g'  -e 's/&#196;//g' \
			-e 's/Ö//g' -e 's/&Ouml;//g'  -e 's/&#214;//g' \
			-e 's/Ü//g' -e 's/&Uuml;//g'  -e 's/&#220;//g' \
			-e 's/ß//g' -e 's/&szlig;//g' -e 's/&#223;//g' \
			> /tmp/tanken1.txt
		
# *-----------------------------------------------------------------------------------*
# *      Daten auswhlen und Liste aufbauen                                           *
# *-----------------------------------------------------------------------------------*
		Head="Tankstellen im Postleitzahlenbereich $PLZ Kraftstoffart: $SPRIT"

		nostations=`sed -n -e "/Abfrage leider keine Tankstelle finden/ ="		/tmp/tanken1.txt`
		if [ "$nostations" != "" ] ; then
			msgbox title="$Head" size=26 timeout=180 msg="Keine Tankstellen gefunden"
			continue
		fi

		zeilen=`sed -n -e "$ ="							/tmp/tanken1.txt`
		tab1=1; tab2=2; tab3=3; tab4=4; step=4

		while [ "$tab4" -le "$zeilen" ]; do
			count=1
			echo ~T0250Tankstelle~T0800Preis > /tmp/tanken2.txt
			echo >> /tmp/tanken2.txt
			while [ $count -le 5 -a "$tab4" -le "$zeilen" ]; do
				a1=`sed -n -e "$tab1"p /tmp/tanken1.txt`; a1=`echo $a1`		# Preis
				a2=`sed -n -e "$tab2"p /tmp/tanken1.txt`; a2=`echo $a2`		# Marke
				a3=`sed -n -e "$tab3"p /tmp/tanken1.txt`; a3=`echo $a3`		# Adresse
				a4=`sed -n -e "$tab4"p /tmp/tanken1.txt`; a4=`echo $a4`		# Ort
				if [ "$a1" != "" ] ; then
					echo ~T0250~B$a2~T0800~R$a1 >> /tmp/tanken2.txt
					echo ~T0250$a3              >> /tmp/tanken2.txt
					echo ~T0250$a4              >> /tmp/tanken2.txt
					echo                        >> /tmp/tanken2.txt
					count=`expr "$count" + 1`
				fi
				tab1=`expr "$tab1" + $step`
				tab2=`expr "$tab2" + $step`
				tab3=`expr "$tab3" + $step`
				tab4=`expr "$tab4" + $step`
			done
            
# *-----------------------------------------------------------------------------------*
# *      Daten ausgeben (Framebuffer)                                                 *
# *-----------------------------------------------------------------------------------*

			echo ~T0250~Galle Angaben ohne Gewhr >> /tmp/tanken2.txt
			if [ $tab4 -le $zeilen ] ; then
				msgbox title="$Head" size=26 timeout=180 msg=/tmp/tanken2.txt
			else
				msgbox title="$Head" size=26 timeout=180 popup=/tmp/tanken2.txt
			fi
			Beenden
		done
	#done
#done

# *-----------------------------------------------------------------------------------*
# *      TMP-Dateien loeschen                                                         *
# *-----------------------------------------------------------------------------------*
rm /tmp/tanken*.txt
